package com.hongkuncheng.cms.controller.index;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.CookieHelper;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.helper.StringHelper;
import com.hongkuncheng.cms.model.Leavemessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Transactional
@RequestMapping("leavemessage")
@ResponseBody
public class IleavemessageController extends BaseController {

    @RequestMapping("submit")
    public void submit() {
        String checkcode = CookieHelper.GetCookie(request, "checkcode", true);
        if (checkcode == null) {
            PageHelper.alertAndRedirect("验证码加载失败", "/leavemessage/");
        } else {
            String vcode = request.getParameter("vcode");
            if (!vcode.equalsIgnoreCase(checkcode)) {
                PageHelper.alertAndGoBack("验证码错误，请重新输入。");
            } else {
                String username = StringHelper.escapeXss(request.getParameter("username"));
                String contents = StringHelper.escapeXss(request.getParameter("contents"));
                Leavemessage leavemessage = new Leavemessage();
                leavemessage.setUsername(username);
                leavemessage.setContents(contents);
                leavemessage.setSubtime(new Date());
                leavemessage.setChecked(false);
                factorySession.save(leavemessage);
                PageHelper.alertAndGoBack("留言成功，并等待管理员审核...");
            }
            CookieHelper.RemoveCookie(response, "checkcode");
        }
    }

}
